﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.warehouse
{
    public partial class warehouseGoods : BaseAdmin
    {
        readonly DALWarehouseGoods theWarehouseGoods = new DALWarehouseGoods();
        readonly DALWarehouse theWarehouse = new DALWarehouse();
        readonly DALModelType theModelType = new DALModelType();
        tbWarehouseGoods oWarehouseGoods;
        readonly DALGoods theGoods = new DALGoods();
        tbGoods oGoods;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(ERule.WarehouseManager);
                this.BindModelTypeList();
                this.BindWarehouse();
                this.BuildForm();
               
            }
        }

     


        void BindWarehouse()
        {
            DataTable dt = theWarehouse.GetList(" flag=1").Tables[0];
            if (dt != null)
            {
                this.ddlWarehouse.DataTextField = "warehouseName";
                this.ddlWarehouse.DataValueField = "id";
                this.ddlWarehouse.DataSource = dt;
                this.ddlWarehouse.DataBind();
            }
            this.ddlWarehouse.Items.Insert(0, new ExtAspNet.ListItem("请选择仓库", "-1"));


        }
        void BuildForm()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                oGoods = theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
                if (oGoods != null)
                {
                    this.txtGoodsCode.Text = oGoods.goodsCode;
                    this.lblGoodsName.Label = "商品名称";
                    this.lblGoodsName.Text =  oGoods.goodsName;
                }
            }
        }
        void BindModelTypeList()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                DataTable table = theModelType.GetList("goodsId = " + Request.QueryString["gid"]).Tables[0];
                if (table == null || table.Rows.Count <= 0)
                {
                    this.ddlModelType.Hidden = true;
                    this.ddlModelType.Required = false;
                }
                else
                {
                    this.ddlModelType.DataTextField = "modelTypeName";
                    this.ddlModelType.DataValueField = "modelTypeId";
                    this.ddlModelType.DataSource = table;
                    this.ddlModelType.DataBind();
                    this.ddlModelType.Items.Insert(0, new ExtAspNet.ListItem("请选择型号", "-1"));
                }
            }
            else
            {
                this.ddlModelType.Hidden = true;
                this.ddlModelType.Required = false;
            }
        }


        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            oGoods = theGoods.GetModelByGoodsCode(this.txtGoodsCode.Text.Trim());
            if (oGoods != null)
            {
                oWarehouseGoods = new tbWarehouseGoods();

                oWarehouseGoods.creater = int.Parse(base.AdminUserId);
                oWarehouseGoods.createTime = DateTime.Now;
                oWarehouseGoods.goodsCount = int.Parse(this.txtCount.Text);
                oWarehouseGoods.goodsId = oGoods.goodsId;
                oWarehouseGoods.remark = this.txtRemark.Text.Trim();
                oWarehouseGoods.warehouseId = int.Parse(this.ddlWarehouse.SelectedItem.Value);
                oWarehouseGoods.GoodsBatchNo = this.txtGoodsBatchNo.Text.Trim();
                oWarehouseGoods.costPrice = decimal.Parse(this.txtCostPrice.Text);
                if (this.ddlModelType.Items.Count > 0 && this.ddlModelType.SelectedItem.Value != "-1")
                {
                    oWarehouseGoods.ModelTypeId = int.Parse(this.ddlModelType.SelectedItem.Value);
                }
                if (theWarehouseGoods.Add(oWarehouseGoods) > 0)
                {
                    ExtAspNet.Alert.Show("添加成功！", "", "window.location.href='warehouseGoods.aspx'");
                }
            }
            else
            {
                ExtAspNet.Alert.Show("没有找到商品编号！");
            }
        }
    }
}